package com.openweixin.sheknows.service.impl;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.openweixin.sheknows.common.Pager;
import com.openweixin.sheknows.dao.QuestionDao;
import com.openweixin.sheknows.dao.UserNewAnswerDao;
import com.openweixin.sheknows.domain.Question;
import com.openweixin.sheknows.domain.UserNewScore;
import com.openweixin.sheknows.service.QuestionService;
@Service
public class QuestionServiceImpl extends BaseServiceImpl<Question, Integer> implements QuestionService { 
	@Resource
	public void setBaseDao(QuestionDao dao) {
		super.setBaseDao(dao);
	}

	@Resource
	public UserNewAnswerDao unaDao;
	
	@Override
	public Pager<Question> findQuestionPagerByUser(Pager<Question> pager,
			Integer uID) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Question.class);
		detachedCriteria.add(Restrictions.eq("uid",uID));
		return getBaseDao().findByPager(pager, detachedCriteria);
	}

	@Override
	public Pager<Question> findQuestionPager(Pager<Question> pager) {
		return getBaseDao().findByPager(pager);
	}

	@Override
	public List<Question> findLockQuestionByExtpert(Integer extpertID) {
		return getBaseDao().getList("lockExpert", extpertID);
	}

	@Override
	public boolean lockQuestionByExtpert(Integer qid, Integer extpertID) {
		Question question=getBaseDao().get(qid);
		if( (question.getLockExpert() != null && question.getLockExpert() !=extpertID)   ||  question.getStatus() !=0){
			return false;
		}
		question.setLockExpert(extpertID);
		return true;
	}

	@Override
	public boolean unlockQuestionByExtpert(Integer qid, Integer extpertID) {
		Question question=getBaseDao().get(qid);
		if(question.getLockExpert() != null && question.getLockExpert() !=extpertID ){
			return false;
		}
		question.setLockExpert(null);
		return true;
	}

	@Override
	public Pager<Question> findQuestionPagerByExtpert(Pager<Question> pager,
			Integer extpertID) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Question.class);
		detachedCriteria.add(Restrictions.eq("lockExpert",extpertID));
		return getBaseDao().findByPager(pager, detachedCriteria);
	}

	@Override
	public Question findQuestionById(Integer id) {
		return get(id);
	}

	@Override
	@Transactional
	public Question readQuestionById(Integer id) {
		Question question=load(id);
		if(question.getStatus()==1){
			question.setStatus(2);
		}
		return question;
	}

	@Override
	@Transactional
	public boolean reSubmit(Integer id, Integer uID) {
		Question question=get(id);
		if(question.getStatus() !=2 || question.getUser().getUid() != uID){//不是本人的问题或问题为解答
			return false;
		}
		question.setStatus(0);
		return true;
	}
	
	@Override
	public Pager<Question> findByPager(Pager<Question> pager) {
		super.findByPager(pager);
		List<Question> answerList = pager.getList();
		for (Question question : answerList) {
			Integer uid = question.getUser().getUid();
			Integer qid = question.getQid();
			List<UserNewScore> uns = unaDao.getNewScoreByUidAndQid(uid, qid);
			question.setNewAnswer(uns != null && uns.size() > 0);
		}
		return pager;
	}
}
